#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function

from logger import logger


class Song(object):
    def __init__(self, song_json):
        super(Song, self).__init__()
        self.__json = song_json
        try:
            self._parse(self.__json)
        except Exception as e:
            logger.error(e)

    def _parse(self, song_json):
        if not song_json or not isinstance(song_json, dict):
            return

        self.title = song_json['title'].replace(' ', '_')
        self.speak = song_json['speak'].replace(' ', '_')
        self.backgroud = song_json['background']
        self.cover = song_json['cover']
        self.id = song_json['id']
        self.favnum = song_json['favnum']
        self.content = song_json['content']
        self.play_url = song_json['url']
        # self.mp3_file = '%s-%s.mp3' % (self.title, self.speak)
        self.mp3_file = '%s.mp3' % (self.id)

    @staticmethod
    def parse(song_json):
        return Song(song_json)


class SongList(object):
    def __init__(self):
        super(SongList, self).__init__()
        self._list = []
        self._dict = {}
        self.current_song_id = 0
        # 0:all repeat 1:order 2:shuffle 3:repeat once 4:once
        self.play_style = 0

    def get_songs_length(self):
        return len(self._list)

    def get_songsid_list(self):
        return self._list

    def get_songs_list(self):
        return [self._dict[x] for x in self._list if self._dict.get(x, None)]

    def get_current_song(self):
        return self._dict.get(self.current_song_id, None)

    def set_play_style(self, style):
        if style >= 0 and style <= 4:
            self.play_style = style

    def set_current_song(self, songid):
        self.current_song_id = songid

    def get_song_index(self, song):
        try:
            index = self._list.index(song.id)
            return index
        except ValueError as e:
            logger.error(e)

    def get_next_song(self):
        try:
            current_index = self._list.index(self.current_song_id)
            next_index = current_index + 1
            if next_index >= len(self._list):
                next_index = 0
            next_songid = self._list[next_index]
            return self._dict.get(next_songid, None)
        except ValueError as e:
            logger.error(e)

    def play_all_repeat(self):
        pass

    def get_song_by_index(self, index):
        if index >= 0 and index < len(self._list):
            return self._dict.get(self._list[index], None)

    def get_prev_song(self):
        try:
            current_index = self._list.index(self.current_song_id)
            prev_index = current_index - 1
            if prev_index < 0:
                prev_index = len(self._list) - 1
            prev_songid = self._list[prev_index]
            return self._dict.get(prev_songid, None)
        except ValueError as e:
            logger.error(e)

    def add_songs(self, song_list):
        if not isinstance(song_list, list):
            return

        for s in song_list:
            if s.id not in self._list:
                self._list.append(s.id)
                self._dict[s.id] = s

        if self.current_song_id == 0:
            self.current_song_id = self._list[0]

    def remove_songs(self, song_list):
        if not isinstance(song_list, list):
            return

        for s in song_list:
            self._list.remove(s.id)
            del self._dict[s.id]


if __name__ == '__main__':
    pass
